﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;
using System.Threading;
using CNVP.Framework.Utils;
using CNVP.Config;

namespace CNVP.JShop.Publish
{
    public class UnitPublish
    {
        #region "属性"
        /// <summary>
        /// 发布首页
        /// </summary>
        public bool IsPubIndex { get; set; }
        /// <summary>
        /// 备份首页
        /// </summary>
        public bool IsBakIndex { get; set; }
        /// <summary>
        /// 发布栏目
        /// </summary>
        public bool IsPubColumn { get; set; }
        /// <summary>
        /// 发布文章
        /// </summary>
        public bool IsPubNews { get; set; }
        /// <summary>
        /// 栏目序号
        /// </summary>
        public int ColumnID { get; set; }
        #endregion
        /// <summary>
        /// 脚本代码
        /// </summary>
        private string jsBlock = "<script" + ">document.getElementById(\"main\").style.display = \"block\";<" + "/script>";
        public void StartPublish()
        {
            HttpContext.Current.Response.Clear();
            Public.RewriteHTML();
            HttpContext.Current.Response.Write(jsBlock);
            HttpContext.Current.Response.Flush();

            if (IsPubIndex) { UnitPublishIndex(); }
            if (IsPubColumn) { UnitPublishColumn(); }
            if (IsPubNews) { UnitPublishNews(); }
        }
        /// <summary>
        /// 发布主页
        /// </summary>
        public void UnitPublishIndex()
        {
            string FormPath = HttpContext.Current.Server.MapPath(UIConfig.InstallDir + "Index.html");
            string ToPath = HttpContext.Current.Server.MapPath(UIConfig.InstallDir + "Index.html.bak");

            if (IsBakIndex)
            {
                jsBlock = "<script" + ">SetPorgressBar('正在备份首页文件，请稍后...',0);<" + "/script>";
                HttpContext.Current.Response.Write(jsBlock);
                HttpContext.Current.Response.Flush();
                Thread.Sleep(100);
                jsBlock = "<script" + ">SetPorgressBar('正在备份首页文件，请稍后...',20);<" + "/script>";
                HttpContext.Current.Response.Write(jsBlock);
                HttpContext.Current.Response.Flush();
                Thread.Sleep(100);
                #region "备份文件"
                FileUtils.CopyFile(FormPath, ToPath);
                #endregion
                jsBlock = "<script" + ">SetPorgressBar('正在备份首页文件，请稍后...',50);<" + "/script>";
                HttpContext.Current.Response.Write(jsBlock);
                HttpContext.Current.Response.Flush();
                Thread.Sleep(100);
                jsBlock = "<script" + ">SetPorgressBar('正在备份首页文件，请稍后...',80);<" + "/script>";
                HttpContext.Current.Response.Write(jsBlock);
                HttpContext.Current.Response.Flush();
                Thread.Sleep(100);                
                jsBlock = "<script" + ">SetPorgressBar('首页文件备份操作完成',100);<" + "/script>";
                HttpContext.Current.Response.Write(jsBlock);
                HttpContext.Current.Response.Flush();
                Thread.Sleep(100);
            }
            jsBlock = "<script" + ">SetPorgressBar('正在发布首页，请稍后...',0);<" + "/script>";
            HttpContext.Current.Response.Write(jsBlock);
            HttpContext.Current.Response.Flush();
            Thread.Sleep(100);
            jsBlock = "<script" + ">SetPorgressBar('首页发布操作成功。',20);<" + "/script>";
            HttpContext.Current.Response.Write(jsBlock);
            HttpContext.Current.Response.Flush();
            Thread.Sleep(100);
            #region "生成页面"
            string strTemplate = General.ReadHtml(0, 0);
            CNVP.JShop.Publish.Template bll = new CNVP.JShop.Publish.Template();
            bll.TemplatePublish = true;
            bll.ProjectName = UIConfig.TemplateDir;
            bll.TemplateContent = strTemplate;
            bll.ColumnID = "0";
            bll.ReplaceLabels();
            strTemplate = bll.TemplateContent;
            General.WriteHtml(strTemplate, FormPath);
            #endregion
            jsBlock = "<script" + ">SetPorgressBar('首页发布操作成功。',50);<" + "/script>";
            HttpContext.Current.Response.Write(jsBlock);
            HttpContext.Current.Response.Flush();
            Thread.Sleep(100);
            jsBlock = "<script" + ">SetPorgressBar('首页发布操作成功。',80);<" + "/script>";
            HttpContext.Current.Response.Write(jsBlock);
            HttpContext.Current.Response.Flush();
            Thread.Sleep(100);
            jsBlock = "<script" + ">SetPorgressBar('首页发布操作成功。',100);<" + "/script>";
            HttpContext.Current.Response.Write(jsBlock);
            HttpContext.Current.Response.Flush();
            Thread.Sleep(100);
        }
        /// <summary>
        /// 发布栏目
        /// </summary>
        public void UnitPublishColumn()
        {
            jsBlock = "<script" + ">SetPorgressBar('正在发布栏目页面，请稍后...',0);<" + "/script>";
            HttpContext.Current.Response.Write(jsBlock);
            HttpContext.Current.Response.Flush();
            Thread.Sleep(100);

            DataTable Dt =CommonData.GetAllColumnList();
            jsBlock =string.Format("<script" + ">SetPorgressBar('共{0}个需要被发布，请稍后...',0);<" + "/script>",Dt.Rows.Count);
            HttpContext.Current.Response.Write(jsBlock);
            HttpContext.Current.Response.Flush();
            Thread.Sleep(100);

            for(int i=0;i<Dt.Rows.Count;i++)
            {
                string _ColumnName = Dt.Rows[i]["ColumnName"].ToString();
                string _ColumnCount = Dt.Rows.Count.ToString();
                jsBlock = string.Format("<script" + ">SetPorgressBar('[{0}]正在发布，第{1}个/共{2}个...',0);<" + "/script>", _ColumnName, i+1, _ColumnCount);
                HttpContext.Current.Response.Write(jsBlock);
                HttpContext.Current.Response.Flush();
                Thread.Sleep(100);

                jsBlock = string.Format("<script" + ">SetPorgressBar('[{0}]正在发布，第{1}个/共{2}个...',25);<" + "/script>", _ColumnName, i + 1, _ColumnCount);
                HttpContext.Current.Response.Write(jsBlock);
                HttpContext.Current.Response.Flush();
                Thread.Sleep(100);
                #region "生成页面"
                string FormPath = HttpContext.Current.Server.MapPath(UIConfig.InstallDir + "Col/Col" + Dt.Rows[i]["ColumnID"] + "/Index.html");
                string strTemplate = General.ReadHtml(Convert.ToInt32(Dt.Rows[i]["ColumnID"]), 0);
                CNVP.JShop.Publish.Template bll = new CNVP.JShop.Publish.Template();
                bll.TemplatePublish = true;
                bll.ProjectName = UIConfig.TemplateDir;
                bll.TemplateContent = strTemplate;
                bll.ColumnID = Dt.Rows[i]["ColumnID"].ToString();
                bll.ReplaceLabels();
                strTemplate = bll.TemplateContent;
                General.WriteHtml(strTemplate, FormPath);
                #endregion
                jsBlock = string.Format("<script" + ">SetPorgressBar('[{0}]正在发布，第{1}个/共{2}个...',50);<" + "/script>", _ColumnName, i + 1, _ColumnCount);
                HttpContext.Current.Response.Write(jsBlock);
                HttpContext.Current.Response.Flush();
                Thread.Sleep(100);

                jsBlock = string.Format("<script" + ">SetPorgressBar('[{0}]正在发布，第{1}个/共{2}个...',75);<" + "/script>", _ColumnName, i + 1, _ColumnCount);
                HttpContext.Current.Response.Write(jsBlock);
                HttpContext.Current.Response.Flush();
                Thread.Sleep(100);

                jsBlock = string.Format("<script" + ">SetPorgressBar('[{0}]发布成功，第{1}个/共{2}个...',100);<" + "/script>", _ColumnName, i + 1, _ColumnCount);
                HttpContext.Current.Response.Write(jsBlock);
                HttpContext.Current.Response.Flush();
                Thread.Sleep(100);
            }
            jsBlock = "<script" + ">SetPorgressBar('栏目页面发布操作成功。',100);<" + "/script>";
            HttpContext.Current.Response.Write(jsBlock);
            HttpContext.Current.Response.Flush();
            Thread.Sleep(500);
        }
        /// <summary>
        /// 发布文章
        /// </summary>
        public void UnitPublishNews()
        {
 
        }
    }
}